<template>
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini"
                     @click="handleClick"   >循环盘点</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="id"
            label="id"
            width="100">
        </el-table-column>
        <el-table-column
            prop="cropName"
            label="作物名称"
            width="100">
        </el-table-column>
        <el-table-column
            prop="cropId"
            label="作物编号"
            width="100">
        </el-table-column>
        <el-table-column
            prop="inventoryUser"
            label="盘点人"
            width="150">
        </el-table-column>
        <el-table-column
            prop="status"
            label="盘点状态"
            width="150">
          <template v-slot="s">
            <span v-if="s.row.status==0">未盘点</span>
            <span v-if="s.row.status==1">已盘点</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="title"
            label="盘点结果"
            width="150">
        </el-table-column>

        <el-table-column
            prop="cropDataSum"
            label="库存数量"
            width="150">
        </el-table-column>
        <el-table-column
            prop="cropRealitySum"
            label="爬电数量"
            width="150">
          <template v-slot="s">
            <el-input-number v-model="s.row.cropRealitySum" @change="handleChange" :min="1" label="描述文字"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">
               <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-edit"
                   @click="gopan(scope.row)"
                   v-hasPermi="['system:dict:edit']"
               >去盘点</el-button>
            </span>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="dayin(scope.row)"
                v-hasPermi="['system:dict:edit']"
            >打印盘点单</el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-pagination
          @current-change="gopage"
          :page-size="size"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>


    </div>
</template>

<script>


import axios from "axios";

export default {
  name: "DingpanstockView.vue",
  data(){
    return{
      tableData:[],
      size:3,
      total:0,
      current:1,
      isButtonDisabled:false,
    }
  },methods:{
    dayin(row){
      let params = new URLSearchParams();
      for (let key in row) {
        if (Object.prototype.hasOwnProperty.call(row, key)) {
          params.append(key, row[key]);
        }
      }
      location.href="http://localhost:10010/cropservice/crop/dayin?id="+row.id;
    },
    gopan(row){
      console.log(row);
      this.axios.post("/cropservice/crop/gopan",row).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.list();
        }
      })
    },
    gopage(current){
      this.current=current;
      this.list();
    },
    list(){
      this.axios.post("/cropservice/crop/xunpanlist?current="+this.current+"&size="+this.size).then(res=>{
        if(res.data.code==200){
          this.tableData=res.data.data.list;
          this.total=res.data.data.total;
        }
      })
    },
    startPolling() {
      this.pollInterval = setInterval(() => {
        console.log('Polling for latest result...');
        this.fetchLatestResult();
      }, 50000);
    },
    async fetchLatestResult() {
      try {
        const response = await axios.post('/cropservice/crop/xlatest');
        const data = response.data;
        console.log("cdvknvkfnvfnvkf"+JSON.stringify(data, null, 2));
        this.hasNewResult = data.data.hasNewResult; // 假设API返回的对象中包含这个字段
        this.message = data.data.message; // 假设API返回的对象中包含这个字段
        if (this.hasNewResult) {
          // 显示通知或执行其他逻辑
          alert(data.data.message);
        }
      } catch (error) {
        console.error('Error fetching result:', error);
      }
    },
  },created() {
    this.list();
    this.startPolling();
  },
  beforeDestroy() {
    // 在组件销毁前清除定时器，避免内存泄漏
    clearInterval(this.pollInterval);
  },

}
</script>

<style scoped>

</style>